Skip to main content

Missing Number

Question

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

Example 1


Input: [3,0,1]
Output: 2

Solution

all//Missing Number.py
def find_missing_number(numbers):
n = len(numbers)
total = (n + 1) * (n + 2) / 2
sum_of_numbers = sum(numbers)
missing_number = total - sum_of_numbers

return int(missing_number)


numbers = [1, 2, 3, 5]
result = find_missing_number(numbers)

print(result)